package cn.cutie.clotcache.core;

import cn.cutie.clotcache.command.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description: 命令存放
 * @Author: Cutie
 * @CreateDate: 2024/6/19 20:16
 * @Version: 0.0.1
 */
public class Commands {
    private static Map<String, Command> All = new LinkedHashMap<>();

    static {
        initCommands();
    }

    private static void initCommands() {
        // 也可以通过@Autowired注解自动注册

        // common
        register(new PingCommand());
        register(new InfoCommand());
        register(new CommandCommand());

        // string
        register(new SetCommand());
        register(new GetCommand());
        register(new StrLenCommand());
        register(new DelCommand());
        register(new ExistsCommand());
        register(new MgetCommand());
        register(new MsetCommand());
        register(new IncrCommand());
        register(new DecrCommand());

        // Lpush, Rpush, Lpop, Rpop, Llen, Lindex, Lrange
        register(new LpushCommand());
        register(new LpopCommand());
        register(new RpushCommand());
        register(new RpopCommand());
        register(new LlenCommand());
        register(new LindexCommand());
        register(new LrangeCommand());


    }

    public static void register(Command command){
        All.put(command.name(), command);
    }

    public static Command get(String name){
        return All.get(name);
    }

    public static String[] getCommandNames(){
//        return All.keySet().toArray(new String[All.size()]);
        return All.keySet().toArray(new String[0]);
    }

}
